package com.pktechan.api.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.ShopCart;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.ShopCartService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.vo.ShopCartVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 购物车相关功能
 * @author: s.zhang
 * @create: 2021-03-05 14:44
 **/
@RestController
@RequestMapping("shopCartController")
public class ShopCartController {
    private static final Logger logger = LoggerFactory.getLogger(ShopCartController.class);

    @Autowired
    private ShopCartService shopCartService;

    /**
     * 根据id查询购物车
     *
     * @param cartId
     * @param response
     */
    @RequestMapping(value = "getShopCartById", method = RequestMethod.POST)
    public void getShopCartById(@RequestParam Integer cartId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + cartId + "查询购物车");
            ShopCart shopCart = shopCartService.getById(cartId);
            HtmlUtil.writerSuccessJson(response, shopCart);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 购物车列表
     *
     * @param shopCart
     * @param response
     */
    @RequestMapping(value = "getShopCartList", method = RequestMethod.POST)
    public void getShopCartList(@RequestBody ShopCart shopCart, HttpServletResponse response) {
        try {
            logger.info("购物车列表");
            PageHelper.startPage(shopCart.getPageNo(), shopCart.getPageSize());
            List<ShopCartVO> list = shopCartService.getShopCartList(shopCart);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到购物车
     *
     * @param shopCart
     * @param response
     */
    @RequestMapping(value = "saveShopCart", method = RequestMethod.POST)
    public void saveShopCart(@RequestBody ShopCart shopCart, HttpServletResponse response) {
        try {
            logger.info("添加到购物车");
            shopCartService.saveOrUpdate(shopCart);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改购物车
     *
     * @param shopCart
     * @param response
     */
    @RequestMapping(value = "updateShopCartById", method = RequestMethod.POST)
    public void updateShopCartById(@RequestBody ShopCart shopCart, HttpServletResponse response) {
        try {
            logger.info("根据id=" + shopCart.getId() + "修改购物车");
            shopCartService.update(shopCart);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id删除购物车
     *
     * @param cartId
     * @param response
     */
    @RequestMapping(value = "deleteShopCartById", method = RequestMethod.POST)
    public void deleteShopCartById(@RequestParam Integer cartId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + cartId + "删除购物车");
            shopCartService.deleteById(cartId);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
